﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace SP_XHWeb.ControlTemplates.SP_XHWeb
{
    public partial class TopNotice : UserControl
    {
        private string DispFormUrl = "/_layouts/SP_XHWeb/DispFormItem.aspx?1=1";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblGroupNotice.InnerHtml = InitGroupTopNotice();
                lblProjectNotice.InnerHtml = InitProjectTopNotice();
            }
        }

        private string InitGroupTopNotice()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> groupLists = GetGroupTopNotice();
            
            //标题长度
            int len = 24;

            str.Append("<ul>");

            if (groupLists != null && groupLists.Count > 5)
            {
                groupLists = (from emp in groupLists orderby emp["创建时间"] descending select emp).Take(5).ToList<SPListItem>();
                for (int i = 0; i < 5; i++)
                {
                    //string[] strs = groupLists[i]["Categories"].ToString().Split('_');
                    //string[] strs = groupLists[i].Url.ToString().Split('/');
                    //string linkUrl = "";
                    //string fileUrl = "";
                    //if (groupLists[i]["Content"] != null && !string.IsNullOrEmpty(groupLists[i]["Content"].ToString()))
                    //{
                    //    string tempUrl = groupLists[i]["Content"].ToString().Substring(groupLists[i]["Content"].ToString().IndexOf('@') + 1);
                    //    fileUrl = tempUrl.Substring(0, tempUrl.LastIndexOf('@'));
                    //}

                    //if (strs.Length == 3)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + strs[1] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[2] + "/" + groupLists[i].Title);
                    //}
                    //else if (strs.Length == 2)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[1] + "/" + groupLists[i].Title);
                    //} 

                    string linkUrl = "/infomationcenter/" + groupLists[i].Url.ToString();
                    str.Append("<li>");
                    if (groupLists[i].Name.Length > len)
                    {
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, groupLists[i].Name.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, groupLists[i].Name);
                    }
                    str.Append("</li>");
                }
            }
            else
            {
                groupLists = (from emp in groupLists orderby emp["创建时间"] descending select emp).ToList<SPListItem>();
                for (int i = 0; i < groupLists.Count; i++)
                {
                    //string[] strs = groupLists[i]["Categories"].ToString().Split('_');
                    //string linkUrl = "";
                    //string fileUrl = "";
                    //if (groupLists[i]["Content"] != null && !string.IsNullOrEmpty(groupLists[i]["Content"].ToString()))
                    //{
                    //    string tempUrl = groupLists[i]["Content"].ToString().Substring(groupLists[i]["Content"].ToString().IndexOf('@') + 1);
                    //    fileUrl = tempUrl.Substring(0, tempUrl.LastIndexOf('@'));
                    //}

                    //if (strs.Length == 3)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + strs[1] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[2] + "/" + groupLists[i].Title);
                    //}
                    //else if (strs.Length == 2)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[1] + "/" + groupLists[i].Title);
                    //} 
                    string linkUrl = "/infomationcenter/" + groupLists[i].Url.ToString();

                    str.Append("<li>");
                    if (groupLists[i].Name.Length > len)
                    {
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, groupLists[i].Name.Substring(0, len) + "...");
                    }
                    else
                    {
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, groupLists[i].Name);
                    }
                    str.Append("</li>");
                }
            }

            str.Append("</ul>");
            return str.ToString();
        }

        private string InitProjectTopNotice()
        {
            StringBuilder str = new StringBuilder();
            List<SPListItem> projectLists = GetProjectTopNotice();
            
            //标题长度
            int len = 18;

            str.Append("<ul>");

            if (projectLists != null && projectLists.Count > 5)
            {
                projectLists = (from emp in projectLists orderby emp["创建时间"] descending select emp).Take(5).ToList<SPListItem>();
                for (int i = 0; i < 5; i++)
                {
                    //string[] strs = projectLists[i]["Categories"].ToString().Split('_');
                    //string[] strcs = projectLists[i]["分类"].ToString().Split('_');
                    //string linkUrl = "";
                    //string fileUrl = "";
                    //if (projectLists[i]["Content"] != null && !string.IsNullOrEmpty(projectLists[i]["Content"].ToString()))
                    //{
                    //    string tempUrl = projectLists[i]["Content"].ToString().Substring(projectLists[i]["Content"].ToString().IndexOf('@') + 1);
                    //    fileUrl = tempUrl.Substring(0, tempUrl.LastIndexOf('@'));
                    //}

                    //if (strs.Length == 3)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + strs[1] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[2] + "/" + projectLists[i].Title);
                    //}
                    //else if (strs.Length == 2)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[1] + "/" + projectLists[i].Title);
                    //} 
                    string listweburl = projectLists[i].ParentList.ParentWeb.Url;
                    string linkUrl = listweburl + "/" + projectLists[i].Url.ToString();


                    str.Append("<li>");
                    if (projectLists[i].Name.Length > len)
                    {
                        //str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + strcs[1].Replace("项目", "") + "】" + projectLists[i].Title.Substring(0, len) + "...");
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + projectLists[i].ParentList.ParentWeb.Title.Replace("项目", "") + "】" + projectLists[i].Name.Substring(0, len) + "...");
                    }
                    else
                    {
                        //str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + strcs[1].Replace("项目", "") + "】" + projectLists[i].Title);
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + projectLists[i].ParentList.ParentWeb.Title.Replace("项目", "") + "】" + projectLists[i].Name);
                    }
                    str.Append("</li>");
                }
            }
            else
            {
                projectLists = (from emp in projectLists orderby emp["创建时间"] descending select emp).ToList<SPListItem>();
                for (int i = 0; i < projectLists.Count; i++)
                {
                    //string[] strs = projectLists[i]["Categories"].ToString().Split('_');
                    //string[] strcs = projectLists[i]["分类"].ToString().Split('_');
                    //string linkUrl = "";
                    //string fileUrl = "";
                    //if (projectLists[i]["Content"] != null && !string.IsNullOrEmpty(projectLists[i]["Content"].ToString()))
                    //{
                    //    string tempUrl = projectLists[i]["Content"].ToString().Substring(projectLists[i]["Content"].ToString().IndexOf('@') + 1);
                    //    fileUrl = tempUrl.Substring(0, tempUrl.LastIndexOf('@'));
                    //}

                    //if (strs.Length == 3)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + strs[1] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[2] + "/" + projectLists[i].Title);
                    //}
                    //else if (strs.Length == 2)
                    //{
                    //    linkUrl = "/" + strs[0] + "/" + (!string.IsNullOrEmpty(fileUrl) ? fileUrl : strs[1] + "/" + projectLists[i].Title);
                    //} 
                    string listweburl = projectLists[i].ParentList.ParentWeb.Url;
                    string linkUrl = listweburl + "/" + projectLists[i].Url.ToString();

                    str.Append("<li>");
                    if (projectLists[i].Name.Length > len)
                    {
                        //str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + strcs[1].Replace("项目", "") + "】" + projectLists[i].Title.Substring(0, len) + "...");
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + projectLists[i].ParentList.ParentWeb.Title.Replace("项目", "") + "】" + projectLists[i].Name.Substring(0, len) + "...");
                    }
                    else
                    {
                        //str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + strcs[1].Replace("项目", "") + "】" + projectLists[i].Title);
                        str.AppendFormat("<a href='{0}' target='_blank'>{1}</a>", linkUrl, "【" + projectLists[i].ParentList.ParentWeb.Title.Replace("项目", "") + "】" + projectLists[i].Name);
                    }
                    str.Append("</li>");
                }
            }

            str.Append("</ul>");
            return str.ToString();
        }


        //资讯中心需要显示的信息
        private List<SPListItem> GetGroupTopNotice()
        {
            List<SPListItem> lists = new List<SPListItem>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                //  http://
                                string listUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                                //using (SPSite site = new SPSite(listUrl + "/default.aspx"))
                                //{
                                //    using (SPWeb web = site.OpenWeb())
                                //    {
                                //        SPList list = web.Lists.TryGetList("ShowNoticeList");
                                //        if (list != null && list.ItemCount != 0)
                                //        {
                                //            foreach (SPListItem item in list.Items)
                                //            {
                                //                if (item.Fields.ContainsField("是否显示"))
                                //                {
                                //                    if (item["是否显示"] != null && !string.IsNullOrEmpty(item["是否显示"].ToString()) && item["是否显示"].ToString() == "显示" && item["Categories"].ToString().Contains("InfomationNotice"))
                                //                    {
                                //                        lists.Add(item);
                                //                    }
                                //                }
                                //            }
                                //        }
                                //    }
                                //}
                                using (SPSite site = new SPSite(listUrl + "/infomationcenter/default.aspx"))
                                {
                                    using (SPWeb web = site.OpenWeb())
                                    {
                                        SPList list = web.Lists.TryGetList("InfomationNotice");
                                        if (list != null && list.ItemCount != 0)
                                        {
                                            foreach (SPListItem item in list.Items)
                                            {
                                                if (item.Fields.ContainsField("首页显示"))
                                                {
                                                    if (item["首页显示"] != null && !string.IsNullOrEmpty(item["首页显示"].ToString()) && item["首页显示"].ToString() == "显示")
                                                    {
                                                        lists.Add(item);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            });
            return lists;
        }

        //项目信息需要显示的信息
        private List<SPListItem> GetProjectTopNotice()
        {
            List<SPListItem> lists = new List<SPListItem>();
            SPSecurity.RunWithElevatedPrivileges(delegate()
                            {
                                //  http://
                                string listUrl = this.Page.Request.Url.Scheme + "://" + this.Page.Request.Url.Authority;
                                //using (SPSite site = new SPSite(listUrl + "/default.aspx"))
                                //{
                                //    using (SPWeb web = site.OpenWeb())
                                //    {
                                //        SPList list = web.Lists.TryGetList("ShowNoticeList");
                                //        if (list != null && list.ItemCount != 0)
                                //        {
                                //            foreach (SPListItem item in list.Items)
                                //            {
                                //                if (item.Fields.ContainsField("是否显示"))
                                //                {
                                //                    if (item["是否显示"] != null && !string.IsNullOrEmpty(item["是否显示"].ToString()) && item["是否显示"].ToString() == "显示" && item["Categories"].ToString().Contains("ProjectNotice"))
                                //                    {
                                //                        lists.Add(item);
                                //                    }
                                //                }
                                //            }
                                //        }
                                //    }
                                //}
                                using (SPSite site = new SPSite(listUrl + "/projectinfo/default.aspx"))
                                {
                                    using (SPWeb pweb = site.OpenWeb())
                                    {
                                        //子站点
                                        if (pweb.Webs.Count != 0)
                                        {
                                            foreach (SPWeb web in pweb.Webs)
                                            {
                                                SPList list = web.Lists.TryGetList("ProjectNotice");
                                                if (list != null && list.ItemCount != 0)
                                                {
                                                    foreach (SPListItem item in list.Items)
                                                    {
                                                        if (item.Fields.ContainsField("首页显示"))
                                                        {
                                                            if (item["首页显示"] != null && !string.IsNullOrEmpty(item["首页显示"].ToString()) && item["首页显示"].ToString() == "显示")
                                                            {
                                                                lists.Add(item);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            });
            return lists;
        }
    }
}
